/*------------------------------------------------------------------------------
 * @file    LOG.C
 * @author  嵌入式王子
 * @email   donminedm@163.com
 * @date    2024/07/24 21:54:57
 * @brief   开源的嵌入式软件包，本人不对此代码负责任
 * -----------------------------------------------------------------------------*/

#include "log.h"
#include <stdint.h>
#include <stdio.h>

#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#define GETCHAR_PROTOTYPE int __io_getchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#define GETCHAR_PROTOTYPE int fgetc(FILE *f)
#endif /* __GNUC__ */

// printf("c") -> __io_putchar('c') -> ch = 'c'
PUTCHAR_PROTOTYPE
{
    // 串口发送函数
    if (HAL_UART_Transmit(&LOG_COM, (uint8_t *)&ch, 1, 0xFFFF) == HAL_OK)
        return ch;
    // 往文件追加内容
    // log_write(ch);
    return -1;
}

// scanf
// getchar() -> __io_getchar() -> 从串口读取一个字符
GETCHAR_PROTOTYPE
{
    char ch;
    // 串口接收函数
    if (HAL_UART_Receive(&LOG_COM, (uint8_t *)&ch, 1, 0xFFFFFFFF) == HAL_OK)
        return ch;
    return -1;
}